package icegps.com.pingdj.activity;


import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import icegps.com.pingdj.R;
import icegps.com.pingdj.listener.AnimListener;
import icegps.com.pingdj.map.MapManager;
import icegps.com.pingdj.map.MapView;
import icegps.com.pingdj.map.listener.OnMapMultipleListener;
import icegps.com.pingdj.map.listener.OnSaveBitmapListener;
import icegps.com.pingdj.utils.UnitUtils;
import icegps.com.pingdj.view.RulerView;

import static icegps.com.pingdj.constant.ActionKey.TITLE;

/**
 * 地图基类  在此将一些共有的特性全搞定了   左侧菜单中的key  右下角的比例尺  左菜单的动画
 */
public abstract class BaseMapActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.map_view)
    MapView mapView;
    @BindView(R.id.tv_enlarge)
    TextView tvEnlarge;
    @BindView(R.id.tv_narrow)
    TextView tvNarrow;
    @BindView(R.id.ll_data_menu)
    LinearLayout llDataMenu;
    @BindView(R.id.iv_left_or_right)
    ImageView ivLeftOrRight;
    @BindView(R.id.recyclerView1)
    RecyclerView recyclerView1;
    @BindView(R.id.tv_ruler)
    TextView tvRuler;
    @BindView(R.id.rulerView)
    RulerView rulerView;
    @BindView(R.id.recyclerView2)
    RecyclerView recyclerView2;
    @BindView(R.id.tv_main_antenna)
    TextView tvMainAntenna;
    @BindView(R.id.iv_main_action)
    ImageView ivMainAction;
    @BindView(R.id.tv_deputy_antenna)
    TextView tvDeputyAntenna;
    @BindView(R.id.iv_deputy_action)
    ImageView ivDeputyAction;
    @BindView(R.id.tv_map_info)
    TextView tvMapInfo;
    private float oldMultiple;
    protected MapManager mapManager;
    private int llDataMenuStart;
    private int llDataMenuEnd;
    private int ivLeftOrRightStart;
    private int ivLeftOrRightEnd;
    private boolean isOpen = true;
    private long token;
    protected String initValue;

    protected static String MAX_HEIGHT;
    protected static String MIN_HEIGHT;
    protected static String AVE_HEIGHT;
    protected static String AREA;
    protected static String SOIL_VOLUME;
    protected static String CURRENT_HEIGHT;

    protected static String TRAGET_HEIGHT;
    protected static String C_H;
    protected static String DIFFERENCE;
    protected static String DISTANCE;
    protected static String BALANCE;
    protected static String CURRENT;
    protected static String SLOPE;
    protected static String SPEED;

    protected String heightUnit;
    protected String lengthUnit;
    protected String areaUnit;
    protected String volumeUnit;
    protected String speedUnit;

    @Override
    protected int setLayout() {
        return R.layout.activity_map;
    }

    @Override
    protected String setTitle() {
        return getIntent().getStringExtra(TITLE);
    }

    @Override
    protected void init() {
        mapManager = new MapManager(mapView, getLoadToken(), getSaveToken());
        initKey();
        initAnimaData();
        setListener();
    }

    protected abstract long getSaveToken();

    protected abstract long getLoadToken();

    private void initKey() {
        MAX_HEIGHT = getResources().getString(R.string.max_height);
        MIN_HEIGHT = getResources().getString(R.string.min_height);
        AVE_HEIGHT = getResources().getString(R.string.ave_height);
        SOIL_VOLUME = getResources().getString(R.string.soil_volume);
        AREA = getResources().getString(R.string.area);
        CURRENT_HEIGHT = getResources().getString(R.string.current_height);
        TRAGET_HEIGHT = getResources().getString(R.string.traget_height);
        C_H = getResources().getString(R.string.c_h);
        DIFFERENCE = getResources().getString(R.string.difference);
        DISTANCE = getResources().getString(R.string.distance);
        BALANCE = getResources().getString(R.string.balance);
        CURRENT = getResources().getString(R.string.current);
        SLOPE = getResources().getString(R.string.slope);
        SPEED = getResources().getString(R.string.speed);
        heightUnit = UnitUtils.index2Value(UnitUtils.HEIGHT, deviceStatus.getHeight());
        lengthUnit = UnitUtils.index2Value(UnitUtils.LENGTH, deviceStatus.getLength());
        areaUnit = UnitUtils.index2Value(UnitUtils.AREA, deviceStatus.getArea());
        volumeUnit = UnitUtils.index2Value(UnitUtils.VOLUME, deviceStatus.getVolume());
        speedUnit = UnitUtils.index2Value(UnitUtils.SPEED, deviceStatus.getSpeed());
        initValue = "0.000";
    }

    private void initAnimaData() {
        llDataMenuStart = 0;
        llDataMenuEnd = getResources().getDimensionPixelOffset(R.dimen.ll_data_menu_width);
        ivLeftOrRightEnd = 0;
        ivLeftOrRightStart = 180;
    }

    private void setListener() {
        tvEnlarge.setOnClickListener(this);
        tvNarrow.setOnClickListener(this);
        ivLeftOrRight.setOnClickListener(this);
        mapView.setOnMapMultipleListener(onMapMultipleListener);
        tvMapInfo.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_enlarge:
                mapManager.enlargeMap();
                break;
            case R.id.tv_narrow:
                mapManager.narrow();
                break;
            case R.id.iv_left_or_right:
                ivLeftOrRight.setEnabled(false);
                dragDataMenu(!isOpen);
                break;
            case R.id.tv_map_info:
                mapManager.showOrHideTextMapInfo(!tvMapInfo.isSelected());
                tvMapInfo.setSelected(!tvMapInfo.isSelected());
                break;
        }
    }

    OnMapMultipleListener onMapMultipleListener = new OnMapMultipleListener() {
        @Override
        public void onMultiple(float multiple) {
            if (oldMultiple != multiple) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (tvRuler != null)
                            tvRuler.setText(Math.round(0.5f / multiple * 200f) + "M");
                        oldMultiple = multiple;
                    }
                });
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapManager.onDestroy();
        onMapMultipleListener = null;
    }

    boolean pauserIsSave = true;

    @Override
    protected void onPause() {
        super.onPause();
        if (pauseIsSaveBitmap() && pauserIsSave)
            mapManager.saveData(null, false);
    }


    @Override
    public void onBackPressed() {
        if (backIsSaveBitmap()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(R.string.exit_and_save_data)
                    .setNegativeButton(R.string.cancel, null)
                    .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            View inflate = View.inflate(activity, R.layout.view_alert, null);
                            TextView tv = inflate.findViewById(R.id.tv_hint);
                            tv.setText(R.string.save_dataing);
                            AlertDialog.Builder b = new AlertDialog.Builder(activity);
                            AlertDialog alertDialog = b.setCancelable(false)
                                    .setView(inflate).create();
                            alertDialog.show();
                            mapManager.saveData(new OnSaveBitmapListener() {
                                @Override
                                public void onSaveBitmapOk(boolean isFinish) {
                                    if (isFinish) {
                                        alertDialog.dismiss();
                                        pauserIsSave = false;
                                        finish();

                                    }
                                }
                            }, true);
                        }
                    }).show();
        } else {
            super.onBackPressed();
        }
    }

    protected abstract boolean pauseIsSaveBitmap();

    protected abstract boolean backIsSaveBitmap();

    private void dragDataMenu(boolean isOpen) {
        this.isOpen = isOpen;
        if (isOpen) {
            llDataMenuStart = -llDataMenu.getWidth() + ivLeftOrRight.getWidth();
            llDataMenuEnd = 0;
            ivLeftOrRightStart = 0;
            ivLeftOrRightEnd = 180;
        } else {
            llDataMenuStart = 0;
            llDataMenuEnd = -llDataMenu.getWidth() + ivLeftOrRight.getWidth();
            ivLeftOrRightStart = 180;
            ivLeftOrRightEnd = 0;
        }
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator objA = ObjectAnimator.ofFloat(llDataMenu, "TranslationX", llDataMenuStart, llDataMenuEnd);
        ObjectAnimator objB = ObjectAnimator.ofFloat(ivLeftOrRight, "Rotation", ivLeftOrRightStart, ivLeftOrRightEnd);
        animatorSet.play(objA);
        animatorSet.play(objB);
        animatorSet.setDuration(200);
        animatorSet.start();
        animatorSet.addListener(new AnimListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                ivLeftOrRight.setEnabled(true);
            }
        });
    }


}
